# coding:utf-8
from django.conf import settings
import re
from django.utils.deprecation import MiddlewareMixin
from django.http import HttpResponseRedirect
from django.shortcuts import render

EXEMPT_URLS=settings.LOGIN_NOT_URL
  
if hasattr(settings,'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]

class LoginRequiredMiddleware(MiddlewareMixin):
    def process_request(self, request):
        if 'user' not in request.session or not request.session['user']:
            path = request.path_info.lstrip('/')
            print path
            flag = True
            for keyword in EXEMPT_URLS:
                if(keyword in path):
                    flag = False
            if(flag):
                return render(request, 'user/signin.html', {'login_msg': 'please login'})
                # return HttpResponseRedirect('/user/signin')
            else:
                pass